package com.bw.z.mvp.view.user;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bw.z.db.DBManager;
import com.bw.z.db.UserTable;
import com.bw.z.db.UserTableDao;
import com.bw.z.mvp.view.activity.LoginActivity;
import com.bw.z.R;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.base.view.BaseFragment;
import com.bw.z.mvp.contract.UserContract;
import com.bw.z.mvp.di.DaggerUserComponent;
import com.bw.z.mvp.di.UserModules;
import com.bw.z.mvp.model.entity.UserItemEntity;
import com.bw.z.mvp.model.entity.UserMenuEntity;
import com.bw.z.mvp.presenter.UserPresenter;
import com.bw.z.mvp.view.user.activity.InformationActivity;
import com.bw.z.mvp.view.user.adapter.UserAdapter;
import com.bw.z.mvp.view.user.adapter.UserMenuAdapter;
import com.bw.z.utils.GlideUtils;
import com.bw.z.utils.SpUtils;

import java.util.List;


public class UserFragment extends BaseFragment<UserPresenter> implements UserContract.IUserView, View.OnClickListener {

    private RecyclerView userRv, menuHeadRv, menuBottomRv;
    private UserAdapter userAdapter;
    private UserMenuAdapter userMenuAdapter;
    private View menuView, timeLineView;
    private TextView menuHeadName;
    private ImageView menuHeadImg;
    private UserBroadcast broadcast;
    private DBManager dbManager;
    private UserTableDao userTableDao;

    @Override
    public int bindLayout() {
        return R.layout.fragment_user;
    }

    @Override
    public void initView() {
        dbManager = new DBManager(getContext());
        userTableDao = dbManager.getDaoSession().getUserTableDao();
        broadcast = new UserBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("nickname");
        intentFilter.addAction("path");
        intentFilter.addAction("set");
        intentFilter.addAction("login_nickname");
        getActivity().registerReceiver(broadcast, intentFilter);

        userRv = f(R.id.user_rv);
        menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_menu, null);
        menuHeadRv = menuView.findViewById(R.id.menu_head_rv);
        menuHeadName = menuView.findViewById(R.id.menu_head_name);
        menuHeadImg = menuView.findViewById(R.id.menu_head_img);

        menuHeadImg.setOnClickListener(this::onClick);
        menuHeadName.setOnClickListener(this::onClick);

        LogUtils.e("table size:" + userTableDao.loadAll().size());
        if (userTableDao.loadAll().size() != 0) {
            UserTable load = userTableDao.load(SpUtils.readDataLong("user_id"));
            if (load != null) {
                menuHeadName.setText(load.getUser_name() + "");
                GlideUtils.getInstance().GlideCircleCropImage(load.getUser_img(), menuHeadImg);
            }
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        menuHeadRv.setLayoutManager(gridLayoutManager);
        userMenuAdapter = new UserMenuAdapter();
        menuHeadRv.setAdapter(userMenuAdapter);

        timeLineView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_menu_sign, null);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        userRv.setLayoutManager(manager);
        userAdapter = new UserAdapter();

        //添加头布局
        userAdapter.addHeaderView(menuView, 0);
        userAdapter.addHeaderView(timeLineView, 1);
        userRv.setAdapter(userAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_head_img:
                toLog();
                break;
            case R.id.menu_head_name:
                toLog();
                break;
        }
    }

    public void toLog() {
        int size = userTableDao.loadAll().size();
        if (size != 0)
            startActivity(new Intent(getContext(), InformationActivity.class));
        else
            startActivity(new Intent(getContext(), LoginActivity.class));
    }

    class UserBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("nickname")) {
                menuHeadName.setText("" + intent.getStringExtra("name"));
            } else if (intent.getAction().equals("login_nickname")) {
                menuHeadName.setText("" + intent.getStringExtra("username"));
            } else if (intent.getAction().equals("path")) {
                GlideUtils.getInstance().GlideCircleCropImage(intent.getStringExtra("path"), menuHeadImg);
            } else if (intent.getAction().equals("set")) {
                menuHeadName.setText("登录/注册");
                Glide.with(getContext())
                        .load(R.mipmap.pn_placeholder)
                        .transform(new CenterCrop(), new CircleCrop()).into(menuHeadImg);
            }
        }
    }

    @Override
    public void inject() {
        DaggerUserComponent.builder().userModules(new UserModules(this)).build().inject(this);
    }

    @Override
    public void initData() {
        p.initData();
    }

    @Override
    public void refresh(BaseEntity entity) {
    }

    @Override
    public void refreshMenu(List<UserMenuEntity> menuEntities) {
        userMenuAdapter.setNewInstance(menuEntities);
    }

    @Override
    public void refreshItem(List<UserItemEntity> itemEntities) {
        userAdapter.setNewInstance(itemEntities);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getContext().unregisterReceiver(broadcast);
    }
}